﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using NHLTester.Classes;
using NHLTester.Forms;

namespace NHLTester
{
    public partial class Form1 : Form
    {
        
        

        public Form1()
        {
            InitializeComponent();
        }

        


        private void button1_Click(object sender, EventArgs e)
        {
            SetFile();
        }

        public void SetFile()
        {
            OpenFileDialog dialog = new OpenFileDialog();
            DialogResult result = dialog.ShowDialog();
            if (result == DialogResult.OK)
                InputFileName.Text = dialog.FileName;
        }

        private List<GameStat> ReadStats(string fileName)
        {
            if (!String.IsNullOrEmpty(fileName))
            {
                List<GameStat> gameResults = new List<GameStat>();
                string[] csvRows = System.IO.File.ReadAllLines(fileName);
                string[] fields = null;
                foreach (string csvRow in csvRows)
                {
                    fields = csvRow.Split(',');
                    GameStat stat = new GameStat
                    {
                        GA = int.Parse(fields[9].ToString()),
                        GF = int.Parse(fields[8].ToString())
                    };
                    gameResults.Add(stat);
                }
                return gameResults;
            }
            return new List<GameStat>();
        }

        
        private void button2_Click(object sender, EventArgs e)
        {
            var stats = ReadStats(InputFileName.Text);
            if (stats.Count>0)
            {
                Symulation sym = new Symulation(GetSettings(), stats, InputFileName.Text);
                SymulationResult result = sym.Start();
                ResultForm f = new ResultForm();
                f.SymulationResults.Add(result);
                f.ShowDialog();
            }
            else
                MessageBox.Show("Nie wybrano pliku do symulacji");
        }

        private SymulationSettings GetSettings()
        {
            SymulationSettings s = new SymulationSettings();
            s.Account = double.Parse(InputStartAccount.Text);
            s.Border = 5.5;
            s.SingleBetPercentage = double.Parse(InputStartPercent.Text);
            s.WinRatio = 1.85;
            s.NumberOfGamesToBreak = int.Parse(InputStartNumberOfBreaks.Text);
            return s;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            List<SymulationResult> output = new List<SymulationResult>();
            if (!String.IsNullOrEmpty(InputDirectoryPath.Text))
            {
                string[] files = Directory.GetFiles(InputDirectoryPath.Text);
                foreach (var file in files)
                {
                    var stats = ReadStats(file);
                    if (stats.Count > 0)
                    {
                        Symulation sym = new Symulation(GetSettings(), stats, Path.GetFileName(file));
                        output.Add(sym.Start());
                    }
                }
                ResultForm f = new ResultForm();
                f.SymulationResults = output;
                f.ShowDialog();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.ShowNewFolderButton = false;
            DialogResult result = dialog.ShowDialog();
            if (result == DialogResult.OK)
                InputDirectoryPath.Text = dialog.SelectedPath;
        }
    }
}
